using System.Collections.Generic;
using System.Linq;
using BCW.Friends;
using I2.Loc;

public class ZombieGameMode : TeamGameMode
{
	public override bool IsAutoStart()
	{
		return true;
	}

	public override bool CanStartNow()
	{
		bool flag = false;
		if (PhotonNetwork.room != null && PhotonNetwork.room.customProperties.ContainsKey("timerStarted"))
		{
			flag = (bool)PhotonNetwork.room.customProperties["timerStarted"];
		}
		if (flag)
		{
			return true;
		}
		int countPlayerForStart = PVPManager.instance.CountPlayerForStart;
		bool flag2 = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.blue && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) >= countPlayerForStart - 1;
		bool flag3 = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.red && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) > 0;
		return (flag2 && flag3) || TDMController.instance.zombieStart == 1;
	}

	public override void StartRound()
	{
		base.StartRound();
		TDMController.instance.RestartTDM();
		MarketController.Instance.SetWeaponSetForZombe(TDMController.myTeam, TDMController.instance.cGraid, true);
		PlayerBehavior.MyPlayer.FirstSpawn(2f);
		CoroutineRunner.Invoke(2.5f, delegate
		{
			if (TDMController.instance != null)
			{
				if (TDMController.instance.CountTeam(PunTeams.Team.red) == 0 && TDMController.myTeam == PunTeams.Team.blue && PhotonNetwork.isMasterClient)
				{
					TDMController.instance.ChangeTeamFromMaster(PlayerBehavior.MyPlayer.photonView.ownerId, PunTeams.Team.red);
					MarketController.Instance.SetWeaponSetForZombe(TDMController.myTeam, TDMController.instance.cGraid, true);
					if (WalkWindow.instance != null)
					{
						WalkWindow.instance.ChangeColor();
					}
				}
				TDMController.instance.SetStartPVP(true);
			}
			if (GameInfoWindow.instance != null)
			{
				GameInfoWindow.instance.HideInterface();
			}
			if (MapWindow.instance != null)
			{
				MapWindow.instance.gameObject.SetActive(true);
			}
			if (GameController.instance != null)
			{
				GameController.instance.showWindowGame();
			}
		});
		ZombieModePlayerStatistics.ResetStatistics();
		ZombieModePlayerStatistics.team = TDMController.myTeam;
		ZombieModePlayerStatistics.enterGameModeTime = (float)PhotonNetwork.time;
	}

	public override void RestartRound()
	{
		TDMController.instance.RestartTDM();
		GameInfoWindow.instance.ShowTDMInfo(true);
		if (FriendsUIGame.instance != null)
		{
			FriendsUIGame.instance.CleanInvitePlayer();
		}
		controllerConnectPhoton.thisScript.LeaveRoom();
	}

	public override void EndRound()
	{
		TDMController.instance.EndZombie();
		GameInfoWindow.instance.ShowShopAndMoney(false, true);
		ZombieModePlayerStatistics.SetSurvivedTime();
	}

	public override string GameModeTitle()
	{
		int num = settings.GetSettings(TypeGame.Zombe).minPlayerToStart - GameController.instance.listPlayers.Count;
		if (num > 0)
		{
			return string.Format(ScriptLocalization.Get("Game_Common/zombie_start"), num);
		}
		return ScriptLocalization.Get("Game_Common/zombie_begin_soon");
	}

	public override string EndGameModeTitle()
	{
		return ScriptLocalization.Get("Game_Common/zombie_over");
	}

	public override TDMResult GetRoundResult()
	{
		List<PlayerBehavior> list = GameController.instance.listPlayers.Where((PlayerBehavior x) => x.localTeam == PunTeams.Team.red).ToList();
		List<PlayerBehavior> list2 = GameController.instance.listPlayers.Where((PlayerBehavior x) => x.localTeam == PunTeams.Team.blue).ToList();
		if (list.Count == 0)
		{
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				return TDMResult.Winner;
			}
			return TDMResult.Loser;
		}
		if (TDMController.myTeam == PunTeams.Team.blue)
		{
			return TDMResult.Winner;
		}
		if (TDMController.myTeam == PunTeams.Team.red)
		{
			if (list2.Count > 0)
			{
				return TDMResult.Loser;
			}
			return TDMResult.Winner;
		}
		return base.GetRoundResult();
	}
}
